﻿//创建时间: 2010-12-15
//创建作者: 李城
//功能说明: JS通用类         
//最后修改: 2010-12-15 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.Collections;
using System.Web;
using System.Net;

namespace TRPOP.Common
{
    /// <summary>
    /// 下载URLfile的方法
    /// </summary>
    public class DownURLFile
    {
        /// <summary>
        /// 读取网络文件字符串
        /// </summary>
        /// <param name="strFileurl">图片网络地址</param>
        /// <returns>如果是图片则下载并返回生成的文件名，否则返回</returns>
        public static string ReadUrlFile(string strFileurl)
        {
            try
            {
                string strResult = string.Empty;
                //请求URL
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strFileurl);
                //设置超时(10秒)
                req.Timeout = 10000;
                //获取Response
                HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
                //创建StreamReader与StreamWriter文件流对象
                StreamReader sr = new StreamReader(rep.GetResponseStream());
                strResult = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
                return strResult;
            }
            catch
            {
                return "无法访问" + strFileurl;
            }
        }

        /// <summary>
        /// 网络图片
        /// </summary>
        /// <param name="strFileurl"></param>
        /// <returns></returns>
        public static StreamReader ReadURLFile(string strFileurl)
        {
            HttpWebRequest oHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(strFileurl);
            oHttpWebRequest.Timeout = 5000;//超时5秒
            try
            {
                HttpWebResponse oHttpWebResponse = (HttpWebResponse)oHttpWebRequest.GetResponse();

                StreamReader oStream = new StreamReader(oHttpWebResponse.GetResponseStream());
                return oStream;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 下载网络图片
        /// </summary>
        /// <param name="strFileurl">图片网络地址</param>
        /// <param name="strFolder">保存到本地图片的文件夹,例如:/img/caiji/</param>
        /// <param name="strFileNameWithoutExtName">文件名,例如:20111201img</param>
        /// <returns>如果是图片则下载并返回生成的文件名，否则返回null</returns>
        public static string DownLoadFile(string strFileurl, string strFolder, string strFileNameWithoutExtName)
        {
            string strFileName = null;
            HttpWebRequest oHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(strFileurl);
            oHttpWebRequest.Timeout = 5000;//超时5秒
            try
            {
                HttpWebResponse oHttpWebResponse = (HttpWebResponse)oHttpWebRequest.GetResponse();
                if (oHttpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    string strFileext = GetExt(oHttpWebResponse.ContentType);
                    if (strFileext != null)
                    {
                        strFileName = strFolder + strFileNameWithoutExtName + "." + strFileext;
                        Stream oStream = oHttpWebResponse.GetResponseStream();
                        //WriteToHDD(System.Web.HttpContext.Current.Server.MapPath(strFileName), oStream);
                        WriteToHDD(Util.GetMapPath(strFileName), oStream);
                        oStream.Close();
                        oStream.Dispose();
                    }
                    oHttpWebResponse.Close();
                }
            }
            catch { }
            return strFileName;
        }
        /// <summary>
        /// 下载网络图片
        /// </summary>
        /// <param name="strFileurl">图片网络地址</param>
        /// <param name="strFolder">保存到本地图片的文件夹,例如:/img/caiji/</param>
        /// <param name="strFileNameWithoutExtName">文件名,例如:20111201img</param>
        /// <param name="oWaterPicLocationEnum">水印图片的位置</param>
        /// <param name="strWaterImg">strWaterImg</param>
        /// <returns></returns>
        public static string DownLoadFile(string strFileurl, string strFolder, string strFileNameWithoutExtName, TRPOP.Common.CMSImageHelper.WaterPicLocation oWaterPicLocationEnum,string strWaterImg)
        {
            string strFileName = null;
            HttpWebRequest oHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(strFileurl);
            oHttpWebRequest.Timeout = 5000;//超时5秒
            try
            {
                HttpWebResponse oHttpWebResponse = (HttpWebResponse)oHttpWebRequest.GetResponse();
                if (oHttpWebResponse.StatusCode == HttpStatusCode.OK)
                {
                    string strFileext = GetExt(oHttpWebResponse.ContentType);
                    if (strFileext != null)
                    {
                        strFileName = strFolder + strFileNameWithoutExtName + "." + strFileext;
                        Stream oStream = oHttpWebResponse.GetResponseStream();
                        //2011.07.09 lcheng 原来的直接保存至硬盘
                        //WriteToHDD(System.Web.HttpContext.Current.Server.MapPath(strFileName), oStream);
                        //2011.07.09 lcheng 根据数据流，将文件流加过水印后保存
                        //CMSImageHelper.DrawWaterImage(oStream, System.Web.HttpContext.Current.Server.MapPath(strFileName), strWaterImg, oWaterPicLocationEnum);
                        CMSImageHelper.DrawWaterImage(oStream, Util.GetMapPath(strFileName), strWaterImg, oWaterPicLocationEnum);
                        
                        oStream.Close();
                        oStream.Dispose();
                    }
                    oHttpWebResponse.Close();
                }
            }
            catch { }
            return strFileName;
        }

        /// <summary>
        /// 将文件流写入硬盘
        /// </summary>
        /// <param name="strFullPath">本地文件的路径</param>
        /// <param name="oStream">文件流</param>
        public static void WriteToHDD(string strFullPath, Stream oStream)
        {
            FileStream writer = new FileStream(strFullPath, FileMode.OpenOrCreate, FileAccess.Write);
            int bufferSize = 512, readSize = 0;
            byte[] buffer = new byte[bufferSize];
            readSize = oStream.Read(buffer, 0, bufferSize);
            while (readSize > 0)
            {
                writer.Write(buffer, 0, readSize);
                readSize = oStream.Read(buffer, 0, bufferSize);
            }
            writer.Flush();
            writer.Close();
        }

        /// <summary>
        /// 通过mime类型获取图片后缀
        /// </summary>
        /// <param name="strMIME"></param>
        /// <returns></returns>
        public static string GetExt(string strMIME)
        {
            switch (strMIME.ToLower().Trim())
            {
                case "image/gif": return "gif";
                case "image/jpeg": return "jpg";
                case "image/png": return "png";
                case "application/x-ms-bmp":
                case "image/nbmp": return "bmp";
                default: return null;
            }
        }

    }
}
